import { Component, OnInit , ChangeDetectorRef } from '@angular/core';

import { NavController } from '@ionic/angular'
import { CommonService }  from '../../service/common.service'
import { StorageService }  from '../../service/storage.service'
import { EventService } from '../../service/event.service'
import { Router } from '@angular/router'


@Component({
  selector: 'app-checkid',
  templateUrl: './checkid.page.html',
  styleUrls: ['./checkid.page.scss'],
})
export class CheckidPage implements OnInit {
  public userInfo:any;
  public name:string = "";
  public code:string ="";
  public phone:string = "";
  public bankcard:string = "";
  public hasbank:string = "自动生成";
  public bankadd:string = "";

  constructor(public router:Router,public eventService:EventService,private nav:NavController,private common:CommonService,private storage:StorageService,private changeDetectorRef:ChangeDetectorRef) { }

  ngOnInit() {
    //初始化用户信息
    this.userInfo = this.common.getInfo() 

    if(this.bankcard.length == 19){
      var apis = "/tbr/getBankName"
      var aparams = {
        bankCard:this.bankcard,	//String	银行卡号
      }
      
      this.common.post(apis,aparams,false).then((response:any)=>{
        console.log(response)
        if(response.code == 200){
          
          this.hasbank = response.msg
          this.changeDetectorRef.detectChanges();
        }
      })
    }

  }

  onChangeText(e){
    if(e.length == 19){
      var apis = "/tbr/getBankName"
      var aparams = {
        bankCard:e,	//String	银行卡号
      }
      
      this.common.post(apis,aparams,false).then((response:any)=>{
        if(response.code == 200){
          console.log(response)
          this.hasbank = response.msg
          this.changeDetectorRef.detectChanges();
        }
      })
    }
  }

  // 点击完成
  onFinish(){
    // 校验姓名
    if(this.common.yzName(this.name) == false){
      return false
    }

    // 校验身份证
    if(this.common.yzCode(this.code) == false){
      return false
    }

    // 校验手机
    if(this.common.yzShouJi(this.phone) == false){
      return false
    }

    // 校验银行号
    if(this.common.bankCard(this.bankcard) == false){
      return false
    }

    // 校验银行
    if(this.hasbank == ""){
      this.common.presentAlert("银行名称不能为空")
      return false
    }

    // 校验银行号
    if(this.bankadd == ""){
      this.common.presentAlert("开户行地址不可为空")
    }
    var api = "/tbr/authentication"
    var params = {
      bankCard:this.bankcard,	//String	银行卡号
      bankName:this.hasbank,	//String	银行名称
      bankOfDeposit:this.bankadd,	//String	开户行
      idCard:this.code,	//String	身份证号码
      name:this.name,	//String	真实姓名
      phone:this.phone,	//String	手机号码
      userId:this.userInfo.userId  ,	//Integer	用户Id
      identityImgFront:'x1',	//String	身份证图片正面地址
      identityImgReverse:'x2',	//String	身份证反面正面地址
    }
    this.common.post(api,params,false).then((response:any)=>{
      if(response.code == 200){
        //通知用户中心更新用户信息
        this.events()
        this.common.presentToast(response.msg)
        this.router.navigateByUrl('/tabs/tab3')
      }
    })
  }
  //通知用户中心更新用户信息
  events(){
    this.eventService.event.emit('userstate');
  }

}
